package com.xyht.sca_s.student_manage_system.modules.dormitory.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryAddReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryInfoExportReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.service.SmsDormitoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 宿舍管理表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-01-08
 */
@RestController
@RequestMapping("/dormitory")
@SecretBody
public class SmsDormitoryController {

    
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private SmsDormitoryService smsDormitoryService;

    @ApiOperation("添加宿舍")
    @PostMapping("/add")
    public ResponseResult addDormitory(HttpServletRequest request, @RequestBody SmsDormitoryAddReq addReq){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsDormitoryService.addDormitory(userId,addReq);
    }

    @ApiOperation("删除宿舍")
    @PostMapping("/deleted")
    public ResponseResult deletedDormitory(@RequestBody SmsDormitoryDeletedReq deletedReq){
        return smsDormitoryService.deletedDormitory(deletedReq);
    }

    @ApiOperation("修改宿舍")
    @PostMapping("/update")
    public ResponseResult updateDormitory(HttpServletRequest request,@RequestBody SmsDormitoryUpdateReq updateReq){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsDormitoryService.updateDormitory(updateReq,userId);
    }

    @ApiOperation("查询宿舍列表")
    @GetMapping("/getDormitoryList")
    public ResponseResult getDormitoryList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "floorId", required = false) String floorId,
                                           @RequestParam(value = "dormitoryId", required = false) String dormitoryId,
                                           @RequestParam(value = "buildId", required = false) String buildId,
                                           @RequestParam(value = "buildingDescription", required = false) String buildingDescription,
                                           @RequestParam(value = "sex", required = false) Integer sex,
                                           @RequestParam(value = "bedNumber", required = false) Integer bedNumber,
                                           @RequestParam(value = "name", required = false) String name,
                                           @RequestParam(value = "feeStandards", required = false) String feeStandards) {

        return smsDormitoryService.getDormitoryList(buildId, floorId, dormitoryId, sex, bedNumber, feeStandards, pageNum, pageSize,name,buildingDescription);
    }

    @ApiOperation("楼栋信息导出模版")
    @PostMapping("/batch/export")
    public ResponseResult exportBuildInfo(@RequestBody SmsDormitoryInfoExportReq exportReq,
                                              HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsDormitoryService.exportBuildInfo(exportReq, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation(value = "寝室专业归属批量导入")
    @PostMapping("/batch/add")
    public ResponseResult batchDormitoryOwnership(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = smsDormitoryService.batchDormitoryOwnership(map,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }


}

